package com.ygxsk.carhome.views.picker;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;

import com.ygxsk.carhome.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 年份选择器
 * Created by ycuwq on 17-12-27.
 */
@SuppressWarnings("unused")
public class YearPicker extends WheelPicker<Integer> {

    private int mStartYear, mEndYear;
    private int mSelectedYear;

    private OnYearSelectedListener mOnYearSelectedListener;

    public YearPicker(Context context) {
        this(context, null);
    }

    public YearPicker(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public YearPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initAttrs(context, attrs);

        setItemMaximumWidthText("00000");

        updateYear();

        setSelectedYear(mSelectedYear, false);

        setOnWheelChangeListener(new OnWheelChangeListener<Integer>() {

            @Override
            public void onWheelSelected(Integer item, int position, String itemString) {

                mSelectedYear = item;

                //LogUtils.d("itchen----> YearPicker 选中的年份 --->"+item + "年");

                if (mOnYearSelectedListener != null) {
                    mOnYearSelectedListener.onYearSelected(item, itemString);
                }

            }
        });

    }

    private void initAttrs(Context context, AttributeSet attrs) {

        if (attrs == null) {
            return;
        }

        mSelectedYear = Calendar.getInstance().get(Calendar.YEAR);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.YearPicker);

        //原始代码
        //mStartYear = a.getInteger(R.styleable.YearPicker_startYear, 1900);
        //mEndYear = a.getInteger(R.styleable.YearPicker_endYear, 2100);
        //年份取出-今年的年份
        mEndYear = WeekUtils.getYearSingleForYearDialog();

        mStartYear = a.getInteger(R.styleable.YearPicker_startYear, 2012);
        mEndYear = a.getInteger(R.styleable.YearPicker_endYear, mEndYear);

        int textSize = a.getDimensionPixelSize(R.styleable.YearMonthDayPicker_itemTextSize,
                getResources().getDimensionPixelSize(R.dimen.WheelItemTextSize));

        int textColor = a.getColor(R.styleable.YearMonthDayPicker_itemTextColor, Color.BLACK);

        boolean isTextGradual = a.getBoolean(R.styleable.YearMonthDayPicker_textGradual, true);

        //2020年01月03日，添加配置文件的内容
        //boolean isCyclic = a.getBoolean(R.styleable.YearMonthDayPicker_wheelCyclic, false);

        boolean isCyclic = true;

        int halfVisibleItemCount = a.getInteger(R.styleable.YearMonthDayPicker_halfVisibleItemCount, 2);

        int selectedItemTextColor = a.getColor(R.styleable.YearMonthDayPicker_selectedTextColor, getResources().getColor(R.color.com_ycuwq_datepicker_selectedTextColor));

        int selectedItemTextSize = a.getDimensionPixelSize(R.styleable.YearMonthDayPicker_selectedTextSize, getResources().getDimensionPixelSize(R.dimen.WheelSelectedItemTextSize));

        int itemWidthSpace = a.getDimensionPixelSize(R.styleable.YearMonthDayPicker_itemWidthSpace, getResources().getDimensionPixelOffset(R.dimen.WheelItemWidthSpace));

        int itemHeightSpace = a.getDimensionPixelSize(R.styleable.YearMonthDayPicker_itemHeightSpace, getResources().getDimensionPixelOffset(R.dimen.WheelItemHeightSpace));

        boolean isZoomInSelectedItem = a.getBoolean(R.styleable.YearMonthDayPicker_zoomInSelectedItem, true);

        boolean isShowCurtain = a.getBoolean(R.styleable.YearMonthDayPicker_wheelCurtain, true);

        int curtainColor = a.getColor(R.styleable.YearMonthDayPicker_wheelCurtainColor, Color.WHITE);

        boolean isShowCurtainBorder = a.getBoolean(R.styleable.YearMonthDayPicker_wheelCurtainBorder, true);

        int curtainBorderColor = a.getColor(R.styleable.YearMonthDayPicker_wheelCurtainBorderColor, getResources().getColor(R.color.com_ycuwq_datepicker_divider));

        a.recycle();


        setTextSize(textSize);

        setTextColor(textColor);

        setTextGradual(isTextGradual);

        setCyclic(isCyclic);

        setHalfVisibleItemCount(halfVisibleItemCount);

        setSelectedItemTextColor(selectedItemTextColor);

        setSelectedItemTextSize(selectedItemTextSize);

        setItemWidthSpace(itemWidthSpace);

        setItemHeightSpace(itemHeightSpace);

        setZoomInSelectedItem(isZoomInSelectedItem);

        setShowCurtain(isShowCurtain);

        setCurtainColor(curtainColor);

        setShowCurtainBorder(isShowCurtainBorder);

        setCurtainBorderColor(curtainBorderColor);

    }

    private void updateYear() {

        List<Integer> list = new ArrayList<>();
        List<String> listString = new ArrayList<>();

        for (int i = mStartYear; i <= mEndYear; i++) {
            list.add(i);
            listString.add(i + "年");
        }

        //这样填充年轮
        setDataList(list, listString);
    }

    public void setStartYear(int startYear) {
        mStartYear = startYear;
        updateYear();
        if (mStartYear > mSelectedYear) {
            setSelectedYear(mStartYear, false);
        } else {
            setSelectedYear(mSelectedYear, false);
        }
    }

    public void setEndYear(int endYear) {
        mEndYear = endYear;
        updateYear();
        if (mSelectedYear > endYear) {
            setSelectedYear(mEndYear, false);
        } else {
            setSelectedYear(mSelectedYear, false);
        }
    }

    public void setYear(int startYear, int endYear) {
        setStartYear(startYear);
        setEndYear(endYear);
    }

    public void setSelectedYear(int selectedYear) {
        setSelectedYear(selectedYear, true);
    }

    public void setSelectedYear(int selectedYear, boolean smoothScroll) {
        setCurrentPosition(selectedYear - mStartYear, smoothScroll);
    }

    public int getSelectedYear() {
        return mSelectedYear;
    }

    public void setOnYearSelectedListener(OnYearSelectedListener onYearSelectedListener) {
        mOnYearSelectedListener = onYearSelectedListener;
    }

    public interface OnYearSelectedListener {
        void onYearSelected(int year, String yearString);
    }

}
